package com.jinlejia.erp.ui.new_house.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 一级区域菜单
 */

public class RegionSelect1Adapter extends YfListAdapter<Map<String,Object>> {
    private int cur_position = -1;
    private Map<Integer,Boolean> checkMap = new HashMap<>();
    private Context context;

    public RegionSelect1Adapter(ArrayList<Map<String,Object>> data) {
        super(data);
    }

    public RegionSelect1Adapter(Context context, ArrayList<Map<String,Object>> data) {
        super(data);
        this.context = context;
    }

    @Override
    public int getItemCount() {
        for (int i = 0; i < mData.size(); i++) {
            if (cur_position == i){
                checkMap.put(i,true);
            }else {
                checkMap.put(i,false);
            }
        }
        return super.getItemCount();
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selector_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ((ViewHolder) viewHolder).houseName.setText(mData.get(i).get("countyName").toString());
        if (checkMap.get(i)){
            ((ViewHolder) viewHolder).houseName.setTextColor(context.getResources().getColor(R.color.orange));
            ((ViewHolder) viewHolder).houseName.setChecked(true);
            ((ViewHolder) viewHolder).select_icon.setVisibility(View.VISIBLE);
            ((ViewHolder) viewHolder).layout.setBackgroundResource(R.color.white);
        }else {
            ((ViewHolder) viewHolder).houseName.setTextColor(context.getResources().getColor(R.color.black));
            ((ViewHolder) viewHolder).houseName.setChecked(false);
            ((ViewHolder) viewHolder).select_icon.setVisibility(View.GONE);
            ((ViewHolder) viewHolder).layout.setBackgroundResource(R.drawable.dark_gray_bg);
        }
        viewHolder.itemView.setTag(mData.get(i));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cur_position != -1){
                    checkMap.put(cur_position,false);
                    notifyItemChanged(cur_position);
                    onItemRemoveListener.onItemRemove(mData.get(cur_position));
                }
                if( cur_position != i ){
                    cur_position = i;
                    checkMap.put(i,true);
                    notifyItemChanged(i);
                    onItemSelectedListener.onItemSelected(mData.get(i));
                }else{
                    if( cur_position != -1 )
                    onItemRemoveListener.onItemRemove(mData.get(cur_position));

                    checkMap.put(cur_position,false);
                    notifyItemChanged(cur_position);
                    cur_position = -1;
                }
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder {

        //城市名
        CheckBox houseName;
        //选择图标
        ImageView select_icon;
        //布局
        RelativeLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            houseName = (CheckBox) itemView.findViewById(R.id.id_city_name);
            select_icon = (ImageView) itemView.findViewById(R.id.selector_icon);
            layout = (RelativeLayout) itemView.findViewById(R.id.layout);
        }
    }

    /**
     * 选中
     */

    public interface OnItemSelectedListener {
        void onItemSelected(Map<String, Object> map);
    }

    private OnItemSelectedListener onItemSelectedListener;

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
    }

    /**
     * 移除
     */
    public interface OnItemRemoveListener {
        void onItemRemove(Map<String, Object> map);
    }

    private OnItemRemoveListener onItemRemoveListener;

    public void setOnItemRemoveListener(OnItemRemoveListener listener) {
        this.onItemRemoveListener = listener;
    }
}
